package com.li.controller;

import com.li.pojo.Class;
import com.li.pojo.Student;
import com.li.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collection;

@Controller
public class StudentController {
    @Resource
    StudentService studentService;
   // @RequestMapping(value = "/user/test")
     //public String allStudent(Model model){
       //  Collection<Student> students=studentService.allStudent();
      //   model.addAttribute("students",students);
      //   return "index";
    // }
    @RequestMapping("/students")
    public String list(Model model){
        Collection<Student> students=studentService.allStudent();
        model.addAttribute("students",students);
        return "student/list";
    }
    @GetMapping(value = "/tostudent")
    public String tostudent(Model model){
        //Collection<Class> classes=studentService.allClass();
        //model.addAttribute("classes",classes);
        return "student/add";
    }
    @RequestMapping(value = "/student/add")
    @ResponseBody
    public String addStudent(Student student){
        if(studentService.addStudent(student))return "添加成功";
        return "学号重复，添加失败";
    }
    @RequestMapping(value = "/student/query")
    public String queryStudent(){
        return "";
    }

    @GetMapping("/student/{sno}")
    public String toupdateStudent(
            @PathVariable("sno")String sno,Model model){
        Student student=new Student(sno);
        student=studentService.queryStudent(student);
        model.addAttribute("stu",student);
        //Collection<Class> classes=studentService.allClass();
        //model.addAttribute("classes",classes);
        return "student/update";
    }
    @RequestMapping(value = "/student/update")
    public String updateStudent(Student student){
        studentService.updateStudent(student);
        return "redirect:/students";
    }
    @RequestMapping(value = "/delestudent/{sno}")
    public String deleteStudent(@PathVariable("sno")String sno){
        Student student=new Student(sno);
        studentService.deleteStudent(student);
        return "redirect:/students";
    }
}
